package cn.edu.live.lecloud.factory;

import java.io.File;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.lecloud.api.connect.CreateLive;
import com.lecloud.api.connect.CreateRecTask;
import com.lecloud.api.connect.FinishActivity;
import com.lecloud.api.connect.ModifyActivify;
import com.lecloud.api.connect.ModifyCoverImage;
import com.lecloud.api.connect.ObtainActivityPlayInfo;
import com.lecloud.api.connect.ObtainPlayAddr;
import com.lecloud.api.connect.ObtainPushAddr;
import com.lecloud.api.connect.QueryActivity;
import com.lecloud.api.connect.QueryActivityStreamInfo;

/**
 * 该类准备封装起与乐视云交互的所有接口，作为一个总类调用，然后需要对方法输入参数的判断调整等过程也编写到此类中。
 * 
 * @author zhagz
 * 
 */
public class LeCloudFactory {

	private static final CreateLive createAct = new CreateLive();
	private static final CreateRecTask createRecTask = new CreateRecTask();
	private static final FinishActivity finishAct = new FinishActivity();
	private static final ModifyActivify modifyAct = new ModifyActivify();
	private static final ModifyCoverImage modifyCover = new ModifyCoverImage();
	private static final ObtainActivityPlayInfo obtainActInfo = new ObtainActivityPlayInfo();
	private static final ObtainPlayAddr obtainPlayAddr = new ObtainPlayAddr();
	private static final ObtainPushAddr obtainPushAddr = new ObtainPushAddr();
	private static final QueryActivity queryAct = new QueryActivity();
	private static final QueryActivityStreamInfo queryActStrInfo = new QueryActivityStreamInfo();

	/**
	 * 创建直播活动。
	 * 
	 * @param map
	 * @return activityId
	 */
	public static String createActivity(Map<String, String> map) {
		JSONObject js = createAct.createLiveRoom(map);
		return js.getString("activityId");
	}

	/**
	 * 创建打点录制任务。 原画码率作为输入码率，直播上的码率作为输出码率，执行转码获取打点录制任务。
	 * 
	 * @param activityId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static JSONObject createRecTask(String activityId, long startTime, long endTime) {
		return createRecTask.createRecTask(activityId, startTime, endTime);
	}

	/**
	 * 停止直播活动，停止后不能再次使用该活动进行直播。
	 * 
	 * @param activityId
	 * @return
	 */
	public static boolean finishActivity(String activityId) {
		return finishAct.finishActivity(activityId);
	}

	/**
	 * 修改直播活动。
	 * 
	 * @param map
	 * @return
	 */
	public static boolean modifyActivity(Map<String, String> map) {
		return modifyAct.modifyActivity(map);
	}

	/**
	 * 修改直播活动封面。直接上传一个图片，支持格式jpg、png、gif。
	 * 
	 * @param activityId
	 * @param file
	 * @return coverImgUrl
	 */
	public static String modifyCover(String activityId, File file) {
		JSONObject js = modifyCover.modifyCover(activityId, file);
		return js.getString("coverImgUrl");
	}

	/**
	 * 根据活动ID，查询录制视频的videoId和videoUnique，其中videoUnique组合成播放地址。
	 * 
	 * 返回示例：{"activityId":"1","machineInfo":[{"videoId":
	 * "26967409","videoUnique": "8cb5186bde"},{}]}
	 * 
	 * @param activityId
	 * @return
	 */
	public static JSONObject getPlayInfo(String activityId) {
		return obtainActInfo.getPlayInfo(activityId);
	}

	/**
	 * 获取播放页地址。出错则返回空
	 * 
	 * @param activityId
	 * @return playPageUrl
	 */
	public static String getPlayAddr(String activityId) {
		try {
			JSONObject js = obtainPlayAddr.getPlayAddr(activityId);
			return js.getString("playPageUrl");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取直播推流地址。</br> 1、如果活动安全设置中启用了推流鉴权，则推流地址的有效时间默认为10分钟，也可以单独设置。</br>
	 * 2、如果是同一个IP客户端推同一个流则不受防盗链限制。
	 * 
	 * 返回示例： </br>{ "liveNum":1, "lives":[ { "machine":"1", "pushUrl":
	 * "rtmp://10.154.28.135/live/20150826309000116?sign=63a0b59238ba4db54d82ad2dd9d7e70f&tm=20150826143027"
	 * , "status":"0" } ] }
	 * 
	 * @param activityId
	 * @return
	 */
	public static JSONObject getPushAddr(String activityId) {
		return obtainPushAddr.getPushAddr(activityId);
	}

	/**
	 * 查询活动列表。
	 * 
	 * @param activityId
	 * @param activityName
	 * @param activityStatus
	 * @param offSet
	 * @param fetchSize
	 * @return
	 */
	public static JSONObject searchActivity(String activityId, String activityName, int activityStatus, int offSet,
			int fetchSize) {
		return queryAct.searchActivity(activityId, activityName, activityStatus, offSet, fetchSize);
	}

	/**
	 * 用于第三方播放器获取直播活动的流信息。
	 * 
	 * @param activityId
	 * @return
	 */
	public static JSONObject queryActivityStreamInfo(String activityId) {
		return queryActStrInfo.queryActivityStreamInfo(activityId);
	}

}
